package com.autumn.workflow.command.diagram;

import java.util.List;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.GraphicInfo;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.HistoricProcessInstanceEntity;
import org.activiti.engine.impl.util.ProcessDefinitionUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.autumn.workflow.command.ParamCommand;
import com.autumn.workflow.model.ProcessTrace;
import com.autumn.workflow.model.ProcessTraceNode;

public class ProcessTraceCommand implements ParamCommand<String, ProcessTrace> {

    @Autowired
    private DiagramRepository repository;

    @Override
    public ProcessTrace execute(CommandContext commandContext, String procInstId) {
        ProcessTrace trace = this.repository.queryProcessTrace(procInstId);
        List<ProcessTraceNode> nodes = trace.getNodes();
        if (null != nodes && !nodes.isEmpty()) {
            HistoricProcessInstanceEntity instance = commandContext.getHistoricProcessInstanceEntityManager().findById(procInstId);
            BpmnModel model = ProcessDefinitionUtil.getBpmnModel(instance.getProcessDefinitionId());
            for (ProcessTraceNode node : nodes) {
                GraphicInfo graphicInfo = model.getGraphicInfo(node.getActId());
                node.setGraphicInfo(graphicInfo);
            }
        }
        return trace;
    }

}
